import { Effect, Reducer } from 'umi'
import { GetMenus } from '@/services/crud';
import { MenuDataItem } from '@ant-design/pro-layout'

export interface MenuModelState {
    menuData: MenuDataItem[],
    loading: boolean,
}

export interface MenuModelType {
    namespace: 'AbpMenu';
    state: MenuModelState;
    effects: {
        fetchMenu: Effect;
    };
    reducers: {
        saveMenuData: Reducer<MenuModelState>;
    };
}

const MenuModel: MenuModelType = {
    namespace: 'AbpMenu',
    state: {
        menuData: [],
        loading: true,
    },
    effects: {
        *fetchMenu(_, { call, put }) {
            const response = yield call(GetMenus)
            yield put({
                type: 'saveMenuData',
                payload: response,
            })
        }
    },
    reducers: {
        saveMenuData(state, action) {
            return {
                ...state,
                menuData: action.payload.result,
                loading: false,
            }
        }
    }
}

export default MenuModel